<template>
	<view class="rticleaInfo">
		<block v-if="rticleaData.adminName">
			<swiper-list :swiperList="rticleaData.rticleaImgList" dataType="v"></swiper-list>
			<view class="container">
				<view class="tui-news-title">
					{{rticleaData.rticleaTitle}}
				</view>
				<view class="tui-sub-info">
					<view class="tui-sub-left">
						<text class="tui-author">{{rticleaData.adminName}}</text>
						<text>{{rticleaData.rticleaTime}}</text>
					</view>
					<view class="tui-sub-right">阅读 {{rticleaData.rticleaRead}}</view>
				</view>
				<view class="tui-news-content">
					<view class="tui-article">
						{{rticleaData.rticleaComm}}
					</view>
				</view>
				<view class="tui-news-source">文章分类：{{rticleaData.rticleaType}}</view>
				
				<!-- 评论区 -->
				<view class="comment-section">
					<view class="comment-title">评论区 ({{comments.length}})</view>
					
					<!-- 评论列表 -->
					<view class="comment-list">
						<view class="comment-item" v-for="(item, index) in comments" :key="item.id">
							<image class="comment-avatar" :src="item.userAvatar" mode="aspectFill"></image>
							<view class="comment-content">
								<view class="comment-user">{{item.userName}}</view>
								<view class="comment-text">{{item.content}}</view>
								<view class="comment-time">{{item.createTime}}</view>
							</view>
						</view>
					</view>
					
					<!-- 发表评论 -->
					<view class="comment-input">
						<input 
							type="text" 
							v-model="newComment" 
							placeholder="写下你的评论..." 
							@confirm="submitComment"
						/>
						<button class="submit-btn" @tap="submitComment">发送</button>
					</view>
				</view>
			</view>
		</block>
	</view>
</template>

<script>
	import swiperList from '../../components/swiperList.vue'
	export default {
		components: {
			swiperList
		},
		data() {
			return {
				rticleaId: 1,
				rticleaData: {},
				comments: [],
				newComment: '',
				userInfo: {
					userName: '微信用户',
					userAvatar: '/static/icon/user.png'
				}
			};
		},
		onLoad(option) {
			this.rticleaId = option.rticleaId || 1
			this.getRticleaById()
			this.loadComments()
			this.loadUserInfo()
		},
		methods: {
			getRticleaById() {
				this.$api.getRticleaById({
					rticleaId: this.rticleaId
				}).then(res => {
					if (res.code) {
						this.rticleaData = res.data
						uni.$emit('updataRticleaRead', this.rticleaId, this.rticleaData.rticleaRead)
						 // 保存文章数据到本地存储
						            const key = `rticlea_${this.rticleaId}`
						            uni.setStorageSync(key, JSON.stringify(this.rticleaData))
					}
				})
			},
			
			// 加载评论
			loadComments() {
				const key = `article_comments_${this.rticleaId}`
				const savedComments = uni.getStorageSync(key)
				this.comments = savedComments ? JSON.parse(savedComments) : []
			},
			
			// 加载用户信息
			loadUserInfo() {
				const userInfo = uni.getStorageSync('userInfo')
				if (userInfo) {
					this.userInfo = JSON.parse(userInfo)
				}
			},
			
			// 提交评论
			submitComment() {
				if (!this.newComment.trim()) {
					uni.showToast({
						title: '评论内容不能为空',
						icon: 'none'
					})
					return
				}
				
				const comment = {
					id: Date.now().toString(),
					content: this.newComment,
					userName: this.userInfo.userName,
					userAvatar: this.userInfo.userAvatar,
					createTime: new Date().toLocaleString()
				}
				
				this.comments.unshift(comment)
				
				// 保存到本地存储
				const key = `article_comments_${this.rticleaId}`
				uni.setStorageSync(key, JSON.stringify(this.comments))
				
				// 清空输入
				this.newComment = ''
				
				uni.showToast({
					title: '评论成功',
					icon: 'success'
				})
			}
		}
	}
</script>

<style lang="scss">
.comment-section {
	margin-top: 30rpx;
	padding: 20rpx;
	background-color: #fff;
	
	.comment-title {
		font-size: 32rpx;
		font-weight: bold;
		padding: 20rpx 0;
		border-bottom: 1rpx solid #eee;
	}
	
	.comment-list {
		.comment-item {
			display: flex;
			padding: 20rpx 0;
			border-bottom: 1rpx solid #eee;
			
			.comment-avatar {
				width: 80rpx;
				height: 80rpx;
				border-radius: 50%;
				margin-right: 20rpx;
			}
			
			.comment-content {
				flex: 1;
				
				.comment-user {
					font-size: 28rpx;
					color: #333;
					font-weight: bold;
				}
				
				.comment-text {
					font-size: 28rpx;
					color: #666;
					margin: 10rpx 0;
				}
				
				.comment-time {
					font-size: 24rpx;
					color: #999;
				}
			}
		}
	}
	
	.comment-input {
		display: flex;
		align-items: center;
		padding: 20rpx;
		background-color: #f8f8f8;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		
		input {
			flex: 1;
			height: 70rpx;
			background-color: #fff;
			border-radius: 35rpx;
			padding: 0 30rpx;
			margin-right: 20rpx;
			font-size: 28rpx;
		}
		
		.submit-btn {
			width: 120rpx;
			height: 70rpx;
			line-height: 70rpx;
			text-align: center;
			background-color: #007AFF;
			color: #fff;
			border-radius: 35rpx;
			font-size: 28rpx;
		}
	}
}
.rticleaInfo {
		min-height: 100vh;
		background-color: #FFFFFF;

		.container {
			padding: 40rpx 30rpx 110rpx 30rpx;
			box-sizing: border-box;
		}

		.tui-news-title {
			font-size: 48rpx;
			font-weight: 500;
			text-align: justify;
		}

		.tui-sub-info {
			padding-top: 30rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 28rpx;
			color: #999;
		}

		.tui-news-content {
			padding-top: 40rpx;
		}

		.tui-article {
			/* text-indent: 2em; */
			font-size: 34rpx;
			padding-bottom: 40rpx;
			line-height: 60rpx;
			text-align: justify;
			word-break: break-all;
			word-wrap: break-word;
		}

		.tui-news-source {
			font-size: 24rpx;
			color: #999;
		}

		.tui-article-pic {
			width: 100%;
			display: block;
			margin-bottom: 40rpx;
		}


		.tui-author {
			color: #5677fc;
			padding-right: 20rpx;
		}
	}
// 原有样式保持不变...
</style>


